package com.tss.dao.impl;

import com.tss.dao.DestinationDAO;
import com.tss.model.Destination;
import com.tss.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 目的地信息表操作实现类
 */
public class DestinationDAOImpl implements DestinationDAO {

    @Override
    public Destination selectByCode(String code) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Destination destination = null;
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM destination WHERE destination_code = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, code);
            rs = ps.executeQuery();
            
            if (rs.next()) {
                destination = new Destination();
                destination.setCode(rs.getString("destination_code"));
                destination.setName(rs.getString("destination_name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(null, ps, rs);
        }
        
        return destination;
    }

    @Override
    public List<Destination> selectAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Destination> destinations = new ArrayList<>();
        
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM destination";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            
            while (rs.next()) {
                Destination destination = new Destination();
                destination.setCode(rs.getString("destination_code"));
                destination.setName(rs.getString("destination_name"));
                destinations.add(destination);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(null, ps, rs);
        }
        
        return destinations;
    }
} 